﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using DTO;

namespace DAL
{
    public class SourceService
    {
        CRMEntities db;
        public List<SourceEntity> LoadSourceByAll()
        {
                db = new CRMEntities();
                var query = (from so in db.Sources
                            select new SourceEntity
                            {
                                Source_ID = so.Source_ID,
                                SourceName = so.SourceName,
                                TotalData = so.TotalData,
                                TotalDup = so.TotalDup,
                                TotalDeDup = so.TotalDeDup,
                                TotalWrongData = so.TotalWrongData,
                                ImportDate = so.ImportDate,                                
                            }).ToList();
                return query;
        }

        public List<SourceEntity> LoadSourceForDivide(DateTime startdate, DateTime enddate)
        {
            db = new CRMEntities();
            CAService sv = new CAService();
            List<SourceEntity> newlist = new List<SourceEntity>();
            var query = (from sa in
                            (from so in db.Sources
                             where so.ImportDate >= startdate && so.ImportDate <= enddate
                             select so)
                        where db.Customers.Any(a => a.CallStatus == 5 || a.CallStatus == 2)
                        select new SourceEntity
                        {
                            Source_ID = sa.Source_ID,
                            SourceName = sa.SourceName,
                            ImportDate = sa.ImportDate,                            
                        }).ToList();
            if (query != null && query.Count > 0)
            {
                var list = sv.GetDWorkingList().ToList();
                var newlisttmp = new List<vw_DWorking>();
                foreach (var item in query)
                {
                    if (list.Any(a => a.Source_ID == item.Source_ID))
                    {
                        newlisttmp.AddRange(list.Where(a=> a.Source_ID == item.Source_ID));
                    }
                }
                foreach (vw_DWorking item in newlisttmp.Where(a => a.CusCallStatus == 5 || a.CusCallStatus == 2))
                {
                    if (item.DWCallStatus != null)
                    {
                        item.CountDivided = item.CountAll;
                    }
                    else
                    {
                        item.CountUnDivided = item.CountAll;
                    }
                }
                if (list != null)
                {
                    newlist = list.GroupBy(l => l.Source_ID).Select(cl => new SourceEntity
                    {
                        Source_ID = cl.First().Source_ID,
                        SourceName = query.Where(a => a.Source_ID == cl.First().Source_ID).SingleOrDefault().SourceName,
                        ImportDate = query.Where(a => a.Source_ID == cl.First().Source_ID).SingleOrDefault().ImportDate,
                        Divided = cl.Sum(c => c.CountDivided),
                        UnDivided = cl.Sum(c => c.CountUnDivided),
                    }).ToList();                    
                }
                return newlist;
            }
            else
            {
                return null;
            }
            
        }
    }
}
